<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Code extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('Code_model');
        $this->load->model('Category_model');
        $this->load->model('TAG_model');
    }

    //初始化代码列表
    function index() {
        $data = NULL;

        $this->form_validation->set_rules('username', 'Username', 'required');

        $data['code'] = $this->Code_model->get_coder();
        $data['category'] = $this->Category_model->get_category();
        $this->load->view('list_code_view', $data);
    }

    //Code查询
    function Select_Code() {
        $data = NULL;

        $param = $this->input->post('s_Name');

        $data['code'] = $this->Code_model->get_coder($param);

        $data['category'] = $this->Category_model->get_category();

        $this->load->view('list_code_view', $data);
    }

    //通过代码类别查询
    function Select_Category($id) {
        $data = NULL;
        $data['code'] = $this->Code_model->get_coder_CategoryID($id);

        $data['category'] = $this->Category_model->get_category();

        $this->load->view('list_code_view', $data);
    }

    //初始化代码添加
    function addCode() {
        $data = NULL;
        $data['category'] = $this->Category_model->get_category();
        $this->load->view('add_Code_view', $data);
    }

    //发布代码
    function Insert_Code() {

        $this->lang->load('form_validation', 'chinese');
        //设置验证规则
        $this->form_validation->set_rules('C_Title', '标题', 'required');
        $this->form_validation->set_rules('C_Description', '描述说明', 'required');
        $this->form_validation->set_rules('C_CodeContent', '代码正文', 'required');

        //表单验证
        if ($this->form_validation->run() == FALSE) {
            $data['category'] = $this->Category_model->get_category();
            $data['error'] = "发布错误 请检查填写";
            $this->load->view('add_Code_view', $data);
        } else {

            $data = NULL;
            $TAG = NULL;

            $data = array(
                'C_Title' => $this->input->post('C_Title'),
                'C_Category' => $this->input->post('C_Category'),
                'C_Description' => $this->input->post('C_Description'),
                'C_CodeContent' => $this->input->post('C_CodeContent'),
                'C_Time' => date('Y-m-d H:i:s')
            );

            $this->Code_model->insert_coder($data);
            redirect('Code');
        }
    }

    //显示代码详细内容
    function Show_Code($id) {

        //获取代码库分类
        $data['category'] = $this->Category_model->get_category();

        //查询详细信息
        $data['code'] = $this->Code_model->get_show_coder($id);

        $this->load->view('show_code_view', $data);
    }

    //查看编辑
    function eidtCode($id) {
        //获取代码库分类
        $data['category'] = $this->Category_model->get_category();

        //查询详细信息
        $data['code'] = $this->Code_model->get_show_coder($id);

        $this->load->view('edit_code_view', $data);
    }

    //编辑
    function Eidt_Code() {
        $data = array(
            'C_Title' => $this->input->post('C_Title'), 
            'C_Category' => $this->input->post('C_Category'),
            'C_Description' => $this->input->post('C_Description'),
            'C_CodeContent' => $this->input->post('C_CodeContent'),
            'C_Time' => date('Y-m-d H:i:s')
        );

        $this->Code_model->update_coder($data,$this->input->post('C_Id'));
        redirect('Code');
    }
    
    //删除
    function Del_Code($id){
        $this->Code_model->del_coder($id);
        redirect('Code');
    }

}

?>